﻿using System;
using System.Reflection;
using System.Web.UI;

namespace SharePointPOBox
{
    
    public class PostOfficeBoxUtilControl : Control
    {
        #region properties

        /// <summary>Control id that password input control in same naming context</summary>
        public string PasswordInputControlID
        {
            get
            {
                return (string)this.ViewState["PasswordInputControlID"] ?? string.Empty;
            }
            set
            {
                this.ViewState["PasswordInputControlID"] = value;
            }
        }

        /// <summary>Password input control's property name</summary>
        /// <remarks>Property must return string value</remarks>
        public string PasswordInputControlValuePropertyName
        {
            get
            {
                return (string)this.ViewState["PasswordInputControlValuePropertyName"] ?? string.Empty;
            }
            set
            {
                this.ViewState["PasswordInputControlValuePropertyName"] = value;
            }
        }

        #endregion properties

        #region methods

        /// <summary>Find control by Id.</summary>
        /// <param name="Id">Control ID</param>
        /// <param name="PropertyName">Target Property Name in Target Control</param>
        /// <returns>Target Value</returns>
        private string GetTargetControlValue(string ControlId, string PropertyName)
        {
            if (string.IsNullOrEmpty(ControlId))
            {
                throw new ArgumentNullException("ControlId");
            }

            if (string.IsNullOrEmpty(PropertyName))
            {
                throw new ArgumentNullException("PropertyName");
            }

            Control ctlTarget = this.NamingContainer.FindControl(ControlId);
            if (ctlTarget == null)
            {
                throw new NullReferenceException("TargetControl Not Found.[" + ControlId + "]");
            }

            PropertyInfo propTarget
                = ctlTarget.GetType().GetProperty(PropertyName, typeof(string));

            object objTarget = propTarget.GetValue(ctlTarget, null);

            string strValue = string.Empty;
            if (objTarget != null)
            {
                strValue = objTarget.ToString();
            }
            return strValue;
        }

        #endregion methods
    }
}
